<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrator_administration extends MY_Administrator {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();		
		//Load Model
		$this->load->model('administrator_administration_model');
    }
	
	/*Main Menu*/
	public function mainmenu_management($module=NULL,$arg=NULL)
	{
		switch($module):
			case 'add':
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_administration_model->mainmenu_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
				$this->data['content'] = "administrator/administration/mainmenu_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);			
			break;
			case 'edit':
				$id = intval($arg);
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_administration_model->mainmenu_management_edit($id);
					redirect($this->session->userdata('returnUrl'));		
				endif;
				$this->data['dataResult'] = $this->db->get_where('mainmenu',array('mainmenu_id' => $id),1)->row();
				$this->data['content'] = "administrator/administration/mainmenu_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);				
			break;
			case 'delete':
				$this->administrator_administration_model->mainmenu_management_delete();
				redirect($this->session->userdata('returnUrl'));
			break;
			case 'update':
				$this->administrator_administration_model->mainmenu_management_update();
				redirect($this->session->userdata('returnUrl'));			
			break;									
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));

				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "mainsort_id ASC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case '':
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
				
				
				/*Search Condition*/
				$searchCondition = array('mainmenu_id IS NOT NULL');
				$txt_search = $this->input->get('txt_search');
				if(!empty($txt_search)):
					$searchCondition[] = "main_name LIKE '%".$txt_search."%'";
				endif;
				
			  	/*Pager*/
				$config['base_url'] = current_url().'?txt_search='.$this->input->get('txt_search');
				$config['total_rows'] = $this->db->where(''.implode(' AND ',$searchCondition).'')->count_all_results('mainmenu');
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->order_by($orderCondition)->get_where('mainmenu',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();					
					
				$this->data['content'] = 'administrator/administration/'.$this->uri->rsegment(2);
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);			
		endswitch;
	}
	
	/*Sub Menu*/
	public function submenu_management($module=NULL,$arg=NULL)
	{
		switch($module):
			case 'add':
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_administration_model->submenu_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
				$this->data['dataMainMenu']	 = $this->db->get_where('mainmenu',array('mainmenu_id' => $this->input->get('mainmenu_id')))->row();
				$this->data['content'] = "administrator/administration/submenu_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);			
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_administration_model->submenu_management_edit($id);
					redirect($this->session->userdata('returnUrl'));;			
				endif;

				$this->data['dataResult'] = $this->db->join('mainmenu as j','j.mainmenu_id=t.mainmenu_id','INNER')->get_where('submenu as t',array('t.submenu_id' => $id),1)->row();
				$this->data['content'] = "administrator/administration/submenu_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;
			case 'delete':
				$this->administrator_administration_model->submenu_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;	
			case 'update':
				$this->administrator_administration_model->submenu_management_update();
				redirect($this->session->userdata('returnUrl'));
			break;					
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
				
				if($this->input->get('mainmenu_id')!=''):
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "t.subsort_id ASC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
				
				
				/*Search Condition*/
				$searchCondition = array('t.submenu_id IS NOT NULL');
				$mainmenu_id = $this->input->get('mainmenu_id');
				$txt_search = $this->input->get('txt_search');
				if(!empty($mainmenu_id)):
					$searchCondition[] = "t.mainmenu_id ='".$mainmenu_id."'";
				endif;	
				if(!empty($txt_search)):
					$searchCondition[] = "t.sub_name LIKE '%".$txt_search."%'";
				endif;
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?mainmenu_id='.$this->input->get('mainmenu_id').'&amp;txt_search='.$this->input->get('txt_search');
				$config['total_rows'] = $this->db->join('mainmenu as j','j.mainmenu_id=t.mainmenu_id','INNER')->where(''.implode(' AND ',$searchCondition).'')->count_all_results('submenu as t');
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->select('j.main_name,t.*')->join('mainmenu as j','j.mainmenu_id=t.mainmenu_id','INNER')->order_by($orderCondition)->get_where('submenu as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();	
				else:
					$this->data['pager'] = '';
					$this->data['dataResult'] = '';
				endif;			
				
				$this->data['dataMainMenu']	 = $this->db->order_by('mainsort_id','ASC')->get('mainmenu')->result();
				$this->data['content'] = 'administrator/administration/'.$this->uri->rsegment(2);
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}	
	
	/*Extra Menu*/
	public function extramenu_management($module=NULL,$arg=NULL)
	{
		switch($module):
			case 'add':
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_administration_model->extramenu_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
				$this->data['dataMainMenu']	 = $this->db->get_where('mainmenu',array('mainmenu_id' => $this->input->get('mainmenu_id')))->row();
				$this->data['dataSubMenu']	 = $this->db->get_where('submenu',array('submenu_id' => $this->input->get('submenu_id')))->row();
				$this->data['content'] = "administrator/administration/extramenu_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);			
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_administration_model->extramenu_management_edit($id);
					redirect($this->session->userdata('returnUrl'));;			
				endif;

				$this->data['dataResult'] = $this->db->join('submenu as j','j.submenu_id=t.submenu_id','INNER')->join('mainmenu as k','k.mainmenu_id=t.mainmenu_id','INNER')->get_where('extramenu as t',array('t.extramenu_id' => $id),1)->row();
				$this->data['content'] = "administrator/administration/extramenu_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;
			case 'delete':
				$this->administrator_administration_model->extramenu_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;	
			case 'update':
				$this->administrator_administration_model->extramenu_management_update();
				redirect($this->session->userdata('returnUrl'));
			break;					
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
				
				if($this->input->get('submenu_id')!=''):
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "t.extrasort_id ASC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case '':
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
				
				
				/*Search Condition*/
				$searchCondition = array('t.extramenu_id IS NOT NULL');
				$mainmenu_id = $this->input->get('submenu_id');
				$txt_search = $this->input->get('txt_search');
				if(!empty($mainmenu_id)):
					$searchCondition[] = "t.submenu_id ='".$mainmenu_id."'";
				endif;	
				if(!empty($txt_search)):
					$searchCondition[] = "t.extra_name LIKE '%".$txt_search."%'";
				endif;
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?submenu_id='.$this->input->get('submenu_id').'&amp;txt_search='.$this->input->get('txt_search');
				$config['total_rows'] = $this->db->join('submenu as j',"j.submenu_id=t.submenu_id",'INNER')->where(''.implode(' AND ',$searchCondition).'')->count_all_results('extramenu as t');
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->select('k.main_name,j.sub_name,t.*')->join('submenu as j',"j.submenu_id=t.submenu_id",'INNER')->join('mainmenu as k','k.mainmenu_id=j.mainmenu_id','LEFT')->order_by($orderCondition)->get_where('extramenu as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();
				else:
					$this->data['pager'] = '';
					$this->data['dataResult'] = '';
				endif;								

				$this->data['dataMainMenu']	 = $this->db->join('submenu as j',"t.mainmenu_id=j.mainmenu_id",'INNER')->order_by('mainsort_id','ASC')->get('mainmenu as t')->result();				
				$this->data['dataSubMenu']	 = $this->db->order_by('subsort_id','ASC')->get_where('submenu',array('mainmenu_id' => $this->input->get('mainmenu_id')))->result();
				$this->data['content'] = 'administrator/administration/'.$this->uri->rsegment(2);
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}	
	
	/*Group*/
	public function group_management($module=NULL,$arg=NULL)
	{
		switch($module):
			case 'add':
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_administration_model->group_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
				
				$this->data['dataPermission'] = $this->db->join('submenu as j','j.mainmenu_id=t.mainmenu_id','INNER')->order_by('t.mainsort_id ASC,t.mainmenu_id DESC,j.subsort_id ASC')->get('mainmenu as t')->result();
				$this->data['content'] = "administrator/administration/group_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);			
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_administration_model->group_management_edit($id);
					redirect($this->session->userdata('returnUrl'));;			
				endif;
				
				$this->data['dataPermission'] = $this->db->join('submenu as j','j.mainmenu_id=t.mainmenu_id','INNER')->order_by('t.mainsort_id ASC,t.mainmenu_id DESC,j.subsort_id ASC')->get('mainmenu as t')->result();		
				$this->data['generate_menu'] = $this->administrator_administration_model->generate_menu($id);
				$this->data['dataResult'] = $this->db->get_where('group',array('group_id' => $id),1)->row();
				$this->data['content'] = "administrator/administration/group_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);			
			break;
			case 'delete':
				$this->administrator_administration_model->group_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;	
			case 'update':
				$this->administrator_administration_model->group_management_update();
				redirect($this->session->userdata('returnUrl'));
			break;					
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));

				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "group_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case '':
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
				
				
				/*Search Condition*/
				$searchCondition = array('group_id IS NOT NULL');
				$txt_search = $this->input->get('txt_search');	
				if(!empty($txt_search)):
					$searchCondition[] = "group_name LIKE '%".$txt_search."%'";
				endif;
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?txt_search='.$this->input->get('txt_search');
				$config['total_rows'] = $this->db->where(''.implode(' AND ',$searchCondition).'')->count_all_results('group');
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->order_by($orderCondition)->get_where('group',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();				
					
				$this->data['content'] = 'administrator/administration/'.$this->uri->rsegment(2);
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}	
	
	/*Sync Data*/
	public function sync_data($module=NULL,$arg=NULL)
	{
		$this->data['content'] = 'administrator/administration/'.$this->uri->rsegment(2);
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}
}